package com.dzsw.javabase.compress;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class DeflaterSample {

    public static void main(String[] args) {

        test1();
    }

    private static void test1() {
        JSONObject object = JSONUtil.createObj();
        object.set("name", "Jack");
        object.set("age", 22);
        object.set("birthday", "2020-01-05");
        String jsonStr = JSONUtil.toJsonStr(object);
        String str = compress(jsonStr);
        System.out.println(Base64.getUrlEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)));
        System.out.println(jsonStr);
        System.out.println(str);
        System.out.println(decompress(str));
    }

    public static void test0() {
        String s = "UzV3UjUyCnAMMUAFw4bDpkBhVWNHVcKwfHFqYlFywoZHZnFJflFlfHJGNkQyLTHCpzhVw5XDiBldwoVbUX4uVDvCkAQhDCUhw7nCuBTDuCbClsKUwqTDpsKVZMOmw6clw6fCp8Kkw6LCsQxVIcKKccOmLhDChcKYw74yNB3CnsO+MjEYDsO+w7J1w7QxQMKPwrBhwpoOLcKHwrjCv8OMXQA=";
        byte[] bytes = Base64.getDecoder().decode(s);
        System.out.println(new String(bytes));
        System.out.println(decompress(bytes));
    }

    public static String compress(String rawData) {
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true); // nowrap=true
        try {
            deflater.setInput(rawData.getBytes(StandardCharsets.UTF_8));
            deflater.finish();

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            while (!deflater.finished()) {
                int compressedBytes = deflater.deflate(buffer);
                bos.write(buffer, 0, compressedBytes);
            }
            return Base64.getUrlEncoder().withoutPadding()
                    .encodeToString(bos.toByteArray());
        } finally {
            deflater.end();
        }
    }

    public static String decompress(String compressedData) {
        return decompress(Base64.getUrlDecoder().decode(compressedData));
    }

    public static String decompress(byte[] compressedData) {
        Inflater inflater = new Inflater(true);
        try {
            byte[] compressedBytes = compressedData;
            inflater.setInput(compressedBytes);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            while (!inflater.finished()) {
                try {
                    int decompressedBytes = inflater.inflate(buffer);
                    bos.write(buffer, 0, decompressedBytes);
                } catch (DataFormatException e) {
                    throw new RuntimeException("数据损坏", e);
                }
            }
            return bos.toString(StandardCharsets.UTF_8);
        } finally {
            inflater.end();
        }
    }
}
